package com.nectech.client;

import com.nectech.entity.*;
import com.nectech.util.GameUtil;
import com.nectech.util.ImageUtil;
import com.nectech.util.MapleStoryFrame;
import com.nectech.util.MusicThread;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: 浅小北
 * @ClassName: MapleStoryClient
 * @Description: 启动类
 * @Date: 2021/1/27 10:39
 * @Since: version-1.0
 */
public class MapleStoryClient extends MapleStoryFrame {
    public MapleStoryClient() {
        super.init();
    }

    public Image img = GameUtil.getImage("groundpic/foregrd/foregrd01");
    public Image ladderImage = GameUtil.getImage("groundpic/foregrd/ladder");
    public Image personImage = GameUtil.getImage("person/attribute/wu");
    public List<Arrow> arrowList = new CopyOnWriteArrayList<>();

    public Hero hero = new Hero(this,0,0);

    public List<Power> powerList = new CopyOnWriteArrayList<>();
    public List<Prop> propList = new CopyOnWriteArrayList<>();
    public List<Ground> groundList = new CopyOnWriteArrayList<>();
    public List<Ground> ladderList = new CopyOnWriteArrayList<>();

    public PropPackage propPackage = new PropPackage(this);

    public BackGround backGround = new BackGround();
    public List<Mob> mobList = new CopyOnWriteArrayList<>();
    public Random random = new Random();
    {
        for (int i = 0; i < 6; i++) {
            Mob mob = new Mob(this,random.nextInt(1000) + 642,718,Direction.LEFT);
            mobList.add(mob);
        }
        //mobList.add(new Mob(this,700,520,Direction.LEFT));
    }
    {
        Ground ground01 = new Ground(this,0,525,462,475);
        groundList.add(ground01);
        Ground ground02 = new Ground(this,462,586,182,414);
        groundList.add(ground02);
        Ground ground03 = new Ground(this,642,770,1090,230);
        groundList.add(ground03);
        Ground ground04 = new Ground(this,1730,711,85,289);
        groundList.add(ground04);
        Ground ground05 = new Ground(this,1813,655,87,345);
        groundList.add(ground05);
        Ground ground06 = new Ground(this,504,223,574,106);
        groundList.add(ground06);
        Ground ground07 = new Ground(this,940,463,398,113);
        groundList.add(ground07);
        Ground ground08 = new Ground(this,1380,402,310,108);
        groundList.add(ground08);
    }
    {
        Ground ground01 = new Ground(this,563,216,35,308);
        ladderList.add(ground01);
        Ground ground02 = new Ground(this,993,216,35,198);
        ladderList.add(ground02);
        Ground ground03 = new Ground(this,1103,458,35,244);
        ladderList.add(ground03);
    }



     /**
      * @Author: 浅小北
      * @Description: 重写父类中的init，先调用super，添加一套键盘监听器
      * @Date: 20:35 2021/1/27
      * @Param:
      * @return:
      */
    @Override
    public void init() {
        super.init();
        //添加键盘监听器
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_I) {
                    propPackage.live = !propPackage.live;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);
            }
        });

        new MusicThread("bgmusic/AcientForest.mp3", true).start();
    }

    /**
     * @Author: 浅小北
     * @Description: 重写paint方法，将所有东西画出来
     * @Date: 20:35 2021/1/27
     * @Param:
     * @return:
     */
    @Override
    public void paint(Graphics g) {

        backGround.draw(g);

        Iterator<Ground> itGroundList = groundList.iterator();
        while (itGroundList.hasNext()) {
            Ground ground = itGroundList.next();
            ground.draw(g);
        }
        Iterator<Ground> itLadderList = ladderList.iterator();
        while (itLadderList.hasNext()) {
            Ground ground = itLadderList.next();
            ground.draw(g);
        }
        g.drawImage(img,0,0,null);
        g.drawImage(ladderImage,1103,458,null);



        g.drawImage(personImage,0,854,null);
        hero.draw(g);
        /**
         * 使用迭代器和CopyOnWriteArrayList解决线程非安全导致图片闪烁的问题
         */
        Iterator<Arrow> itArrowList = arrowList.iterator();
        while (itArrowList.hasNext()) {
            Arrow arrow = itArrowList.next();
            arrow.draw(g);
            arrow.hit(mobList);
        }
        Iterator<Mob> itMobList = mobList.iterator();
        while (itMobList.hasNext()) {
            Mob mob = itMobList.next();
            mob.draw(g);
            mob.hit();
        }
        Iterator<Power> itPowerList = powerList.iterator();
        while (itPowerList.hasNext()) {
            Power power = itPowerList.next();
            power.draw(g);
        }

        Iterator<Prop> itPropList = propList.iterator();
        while (itPropList.hasNext()) {
            Prop prop = itPropList.next();
            prop.draw(g);
        }


        Font f = g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,30));
        g.drawString("人物方向:" + hero.dir,100,100);
        g.drawString("人物动作:" + hero.action,100,150);
        g.drawString("人物下蹲与否:" + hero.down,100,200);
        g.drawString("弓箭集合长度:" + this.arrowList.size(),100,250);
        g.drawString("怪物集合长度:" + this.mobList.size(),100,300);
        g.drawString("人物MP:" + hero.MP,100,350);
        g.drawString("人物HP:" + hero.HP,100,400);
        g.drawString("人物EXP:" + hero.EXP,100,450);
        g.setFont(f);

        propPackage.draw(g);
    }




    public static void main(String[] args) {
        new MapleStoryClient().init();
    }

}
